$(function () {
    var customerPool_datagrid =$("#customerPool_datagrid");
    var customerPool_dialog = $("#customerPool_dialog");
    
    //给 a链接统一绑定事件
    $("a[data-cmd]").click(function(){
        var methodName = $(this).data("cmd");
        //调用方法
        methodObj[methodName]();
    });

    customerPool_datagrid.datagrid({
        url: '/potentialStudent/selectByPutPoolState.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '学员 ', width: 100},
            {field: 'trackingTimes', title: '跟踪次数', width: 100},
            {field: 'finalTrackingTime', title: '最后跟踪时间', width: 100},
            {field: 'qq', title: 'QQ ', width: 100},
            {field: 'tel', title: '电话 ', width: 100},
            {field: 'school', title: '学校 ', width: 100},
            {field: 'intentionDegree', title: '意向程度', width: 100},
            {field: 'campus', title: '意向校区', width: 100},
            {field: 'grade', title: '意向班级 ', width: 100,formatter:function (value) {
                    return value ? value.className :"";
                }},
            {field: 'comment', title: '摘要 ', width: 100},
        ]],
        toolbar: '#customerPool_toolbar',
        singleSelect: true,
        rownumbers: true,
        pagination: true,
    });

    customerPool_dialog.dialog({
        width: 720,
        height: 450,
        closed: true,
        resizable: false,
        modal:true,
        draggable:true,
        //清空表单数据
        onClose: function () {
            $("#customerPool_form").form('clear');
        }
    });

    $("#dispatch_dialog").dialog({
        width: 400,
        height: 200,
        closed: true,
        resizable: false,
        modal:true,
        draggable:true,
        //清空表单数据
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    methodObj.save();
                }
            },
            {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    methodObj.cancel();
                }
            }
        ],
        onClose: function () {
            $("#customerPool_form").form('clear');
        }
    });

    //使用一个对象来统一管理实践的方法
    var methodObj = {
        edit: function () {
            //判断是否选中了数据
            var row = customerPool_datagrid.datagrid('getSelected');
            console.log(row);
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            $("#customerPool_form").form('load', row);

            //弹出表格窗口
            customerPool_dialog.dialog("open");

            //设置标题
        },
        //取消操作
        cancel: function () {
            $("#dispatch_dialog").dialog('close');
        },
        view:function () {
                methodObj.edit();
            $("#customerPool_form").find("input").prop("readonly",true);
                $(".easyui-combobox").combobox({
                    readonly:true
                });
            customerPool_dialog.dialog('setTitle', '查看潜在学员');
        },
        refresh:function () {
            customerPool_datagrid.datagrid('reload')
        },
        transfer:function () {
            var row = customerPool_datagrid.datagrid('getSelected');
            console.log(row);
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;
            }
            console.log(row);
            //弹出确认框 确认改变状态
            $.messager.confirm('确认', '您确认要跟进该客户?', function (r) {
                if (r) {
                    //发送请求到后台
                    $.post("/movethehistory/transfer.do",{id:row.id,name:row.name,qq:row.qq,tel:row.tel,salesman:row.salesman},function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '跟进成功', 'info', function () {
                                //刷新当前页面
                                customerPool_datagrid.datagrid('reload');
                                $("#dispatch_dialog").dialog('close')
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    })
                }
            })
        },
        assign:function () {
            var row = customerPool_datagrid.datagrid('getSelected');
            if (!row) {
                //如果没有选中 提示框提示信息
                $.messager.alert("温馨提示", "请选中一条数据", "warning");
                return;

            }

            $("#dispatch_form").form('load',row);

            $("#dispatch_dialog").dialog('open')
        },
        save:function () {
            var row = customerPool_datagrid.datagrid('getSelected');
            var username = $("#saleman").combobox('getValue');
            $.messager.confirm('确认', '您确认要跟进该客户?', function (r) {
                if (r) {
                    //发送请求到后台
                    $.post("/movethehistory/transfer.do",{id:row.id,name:row.name,qq:row.qq,tel:row.tel,salesman:row.salesman,usernameId:username},function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '跟进成功', 'info', function () {
                                //刷新当前页面
                                customerPool_datagrid.datagrid('reload');
                                $("#dispatch_dialog").dialog('close')
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'error');
                        }
                    })
                }
            })
        }
    }
    
});

